<?php

if(!defined('DIR_VSCORE'))
	define('DIR_VSCORE', DIR_LIB.'VsCore'.DIR_SEP);

if(!defined('DIR_JUNONA'))
	define('DIR_JUNONA', DIR_LIB.'Junona'.DIR_SEP);



class JunModel {

	static $_sqlTable = 'models';

	private $_id = 0;

	private $_number = '';
	private $_name = '';

	static function updateNames($id, $number, $name) {
		//return;	// Ne Raboti

		$authId = VsAuth::Instance()->id();

		$q = "UPDATE `".self::$_sqlTable."` SET `number`='".$number."', `name`='".$name."', updated_by=".$authId.", updated_at=NOW() WHERE id=".$id;

		$db = VsDatabase::Instance();
		$db->query($q);
	}
	//==========================================================================================================

	static function remove($id) {
		//return;	// Ne Raboti

		$authId = VsAuth::Instance()->id();

		$q = "UPDATE `".self::$_sqlTable."` SET removed=1, updated_by=".$authId.", updated_at=NOW() WHERE id=".$id;

		$db = VsDatabase::Instance();
		$db->query($q);
	}
	//==============================================================================

	static function makeNew() {
		//$item = &self::__construct();	// ERROR
		$item = new JunModel();

		return $item;
	}
	//============================================================================

	static function makeFromForm($form) {
		//echo "EHO"; exit;
		//$item = &self::__construct();	// ERROR
		$item = new JunModel();

		$item->_number = $form['number'];
		$item->_name = $form['name'];

		return $item;
	}
	//==============================================================================

	static function makeFromDb($arRow) {
		//$item = &self::__construct();	// ERROR
		$item = new JunModel();

		$item->_id = $arRow['id'];

		$item->_number = $arRow['number'];
		$item->_name = $arRow['name'];

		return $item;
	}
	//=============================================================================

	private function __construct() {

	}
	//=============================================================================

	function id() { return $this->_id; }

	function number() { return $this->_number; }
	function name() { return $this->_name; }
	//=============================================================================

	function save() {
		if($this->_id > 0)
			$this->update();
		else
			$this->insert();
	}
	//============================================================================

	function insert() {
		$auth = VsAuth::Instance();
		$q = "INSERT INTO `".self::$_sqlTable."`(`number`, `name`, `added_by`) VALUES('".$this->_number."', '".$this->_name."', '".$auth->id()."')";
		//echo $q; exit;
		
		$db = VsDatabase::Instance();
		$db->query($q);
	}
	//=============================================================================

	function update() {
		return;	// Ne Raboti

		$q = "UPDATE `".self::$_sqlTable."` SET ";

		$db = VsDatabase::Instance();
		$db->query($q);
	}
	//==============================================================================

	private function delete() {
		if($this->_id<1)
			return;

		$q = "DELETE * FROM `".self::$_sqlTable."` WHERE id=".$this->_id;

		$db = VsDatabase::Instance();
		$db->query($q);
	}
	//==============================================================================

};

?>
